package com.yang.zngcxyxkjs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.zngcxyxkjs.mapper.AdminMapper;
import com.yang.zngcxyxkjs.mapper.StudentMapper;
import com.yang.zngcxyxkjs.mapper.TeacherMapper;
import com.yang.zngcxyxkjs.pojo.*;
import com.yang.zngcxyxkjs.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {

    @Autowired(required = false)
    private AdminMapper adminMapper;

    @Autowired
    private AdminService adminService;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @RequestMapping("/adminFaceLogin")
    public String adminFaceLogin(){
        return "admin/adminFaceLogin.html";
    }

    @RequestMapping("/adminFaceIndex")
    public String adminFaceIndex(){
        return "admin/adminIndex.html";
    }

    @RequestMapping("/adminlogin")
    public String toadminLogin(){
        return "admin/adminLogin.html";
    }

    @RequestMapping("/adminLogin")
//    @ResponseBody
    public String adminLogin(String account,String password){
        Admin admin = adminMapper.adminLogin(account,password);
        if(admin!=null){
            LoginLog loginlog = new LoginLog();
            loginlog.setLoginAccount(account);
            adminMapper.insertAdminLoginlog(loginlog);
            return "admin/adminIndex.html";
        }
        else{
            return "error.html";
        }
    }

    @PostMapping("/admin/facerecognition")
    @ResponseBody
    public Admin faceserach(@RequestParam String imgUrl) throws Exception {
        Admin admin = adminService.adminCompareFace(imgUrl);
        LoginLog loginlog = new LoginLog();
        loginlog.setLoginAccount(admin.getAccount());
        adminMapper.insertAdminLoginlog(loginlog);
        return admin;
    }

    @RequestMapping("/getAllStudentsByPages")
    public String getAllStudentsByPages(@RequestParam(defaultValue = "1") Integer currentPageNum, Model model, HttpSession session){
        PageHelper.startPage(currentPageNum,5);
        List<Student> list = adminMapper.selectAllStudents();
        PageInfo<Student> pageInfo = new PageInfo<>(list);
//        System.out.println(pageInfo);

//        Student stu = (Student)session.getAttribute("stu");
//        model.addAttribute("stu",stu);

        model.addAttribute("pageInfo",pageInfo);
        return "admin/getAllStudents.html";
    }

    @RequestMapping("/showStudentInfo")
    public String showStudentInfo(String stuNo,Model model){
        Student student = studentMapper.selectStudentByStuNo(stuNo);
        model.addAttribute("stu",student);
        return "admin/showStudentInfo.html";
    }

    @RequestMapping("/updateStudentByStuNo")
    public String updateStudentByStuNo(Student student,Model model){
        boolean i = studentMapper.updateStudentByStuNo(student);
        model.addAttribute("i",i);
        if (i){
            model.addAttribute("stu",student);
            return "admin/showStudentInfo.html";
        }else{
            return "error.html";
        }
    }

    @RequestMapping("/deleteStudentByStuNo")
    public String deleteStudentByStuNo(String stuNo,Model model){
        boolean i = studentMapper.deleteStudentByStuNo(stuNo);
        model.addAttribute("i",i);
        return "redirect:/getAllStudentsByPages";
    }

    @RequestMapping("/getAllTeachersByPages")
    public String getAllTeachersByPages(@RequestParam(defaultValue = "1") Integer currentPageNum, Model model, HttpSession session){
        PageHelper.startPage(currentPageNum,5);
        List<Teacher> list = adminMapper.selectAllTeachers();
        PageInfo<Teacher> pageInfo = new PageInfo<>(list);
//        System.out.println(pageInfo);

//        Student stu = (Student)session.getAttribute("stu");
//        model.addAttribute("stu",stu);

        model.addAttribute("pageInfo",pageInfo);
        return "admin/getAllTeachers.html";
    }

    @RequestMapping("/showTeacherInfo")
    public String showTeacherInfo(String teaNo,Model model){
        Teacher teacher = teacherMapper.selectTeacherByTeaNo(teaNo);
        model.addAttribute("tea",teacher);
        return "admin/showTeacherInfo.html";
    }

    @RequestMapping("/updateTeacherByTeaNo")
    public String updateTeacherByTeaNo(Teacher teacher,Model model){
        boolean i = teacherMapper.updateTeacherByTeaNo(teacher);
        model.addAttribute("i",i);
        if (i){
            model.addAttribute("tea",teacher);
            return "admin/showTeacherInfo.html";
        }else{
            return "error.html";
        }
    }

    @RequestMapping("/deleteTeacherByTeaNo")
    public String deleteTeacherByTeaNo(String teaNo,Model model){
        boolean i = teacherMapper.deleteTeacherByTeaNo(teaNo);
        model.addAttribute("i",i);
        return "redirect:/getAllTeachersByPages";
    }

    @RequestMapping("/adminlogout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/adminlogin";
    }

}
